import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;


public class S2Grammar {

	private ArrayList<String> tags;
	private String s2Path;
	
	public S2Grammar(String vocabPath, String s2Path){
		tags = new ArrayList<>( new VocabParser(vocabPath).getTags());
		this.s2Path=s2Path;
	}
	
	public void geneerateS2(){
		File file = new File(s2Path);
		
		BufferedWriter writer = null;
		
		try {
			writer = new BufferedWriter(new FileWriter(file));
			writer.write("1	S2\n");
			for(String tag: tags){
				writer.write("1	S2	_"+tag+"\n");
			}
			
			for(String tag: tags){
				
				writer.write("1	_"+tag+"	"+tag+"\n");
				for(String child: tags){
					if(!tag.equals(child)){
						writer.write("1	_"+tag+"	"+tag+" _"+child+"\n");
					}
				}
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(writer!=null){
				try {
					writer.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void main(String[] args){
		if(args.length!=2){
			System.out.println("usage: s2grammar <vocab_file> <s2_generated_file_path");
			return;
		}
		String vocab = args[0];
		String s2file = args[1];
		
		S2Grammar grammar = new S2Grammar(vocab, s2file);
		grammar.geneerateS2();
	}
}
